<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 14-7-30
  Time: 下午1:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%@include file="../../../includes/common.jsp" %>
    <title>
        新增试题
    </title>
    <style type="text/css">
        .text {
            padding: 10px;
            width: 52%;
            height: 520px;
            background-color: #D2B48C;
            float: left;
            margin-top: -20px;
        }

        .task {
            padding: 10px;
            width: 44%;
            height: 520px;
            background-color: #8FBC8F;
            float: right;
            margin-top: -20px;
        }
        .option{
            margin-left: 20px;
        }
    </style>
</head>
<body>
<form id="fileUp" method="post" enctype="multipart/form-data">
    <div class="text">
        <div class="control-group">
            <div class="controls">
                <input type="file" name="upFile" id="upFile"/>
                <input type="button" class="btn btn-primary" onclick="fileUp()" value="上传试题">
            </div>
        </div>
        <textarea style="width: 97%;height: 90%">${content}</textarea>
    </div>
    <div class="task">
        <textarea id="content" style="width: 97%;height: 150px"></textarea>
        <div class="control-group">
            <c:forEach var="typeValue" items="${type}">
                <input type="radio" name="taskType" value="${typeValue.dictCode}"/>${typeValue.dictValue}
            </c:forEach>
            <a class="btn btn-primary" onclick="analyze()">解析</a>
            <a class="btn btn-primary" onclick="save()">保存</a>
        </div>
        知识点：<input type="text" id="knowledgePoint"/>
        <fieldset>
            <legend>解析结果</legend>
            <p id="allTitle"><span id="title"></span><span id="answerSpan"></span></p>
            <p id="option">
            </p>
        </fieldset>
    </div>
</form>


</body>
</html>
<script type="text/javascript">
    function fileUp() {
        $("#fileUp").attr("action", basePath + "task/library/addTaskDo").submit();
    }

    function analyze() {
        if($(":radio:checked").val()==null){
            $.messager.alert("提示", "请选择解析类型！", "", function () {})
            return;
        }
        $.ajax({
            url: basePath + "task/library/analyzeLibraryTask",
            data: {
                collectOptions: $("#content").val(),
                taskType: $(":radio:checked").val()
            },
            dataType: "json",
            type: "POST",
            success: function (data) {
                var optionContent =data.collectOptions.split("@#");
                var content="";
                for(var i = 0; i<optionContent.length;i++){
                    content += "<p name='options' class='option'>" + optionContent[i] + "</p>"
                }
                $("#title").html(data.title);
                if(data.taskType==1){
                    $("#answerSpan").html("(答案：<textarea id='answer' style='width: 40px;height: 20px'> </textarea>)");
                    $("#option").html(content);
                }else{
                    $("#answerSpan").html("</br>(答案：<textarea id='answer' style='width: 80px;height: 40px'> </textarea>)");
                }
            },
            error: doError
        })
    }

    function save() {
        var allBox = $("[name='options']");
        var value = "";
        for (var i = 0; i < allBox.length; i++) {
            value += allBox[i].textContent + "@#";
        }
        value = value.substring(0,value.length-2);
        if ($("#title").html()==""){
            $.messager.alert("提示", "请填写试题内容！", "", function () {})
            return;
        }
        if ($("#knowledgePoint").val()==""){
            $.messager.alert("提示", "请填写知识点！", "", function () {})
            return;
        }
        $.ajax({
            url: basePath + "task/library/addLibraryTask",
            data: {
                taskType: $(":radio:checked").val(),
                title: $("#title").html(),
                collectOptions: value,
                answer: $("#answer").val().trim(),
                knowledgePoint: $("#knowledgePoint").val().trim()
            },
            dataType: "json",
            type: "POST",
            success: function () {
                $.messager.alert("成功", "保存成功", "", function () {
                    $("#title").html("");
                    $("#answerSpan").html("");
                    $("#option").html("");
                })
            },
            error: doError
        })
    }
</script>